<?php

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use DiDom\Document;


$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'http://www.bjbus.com/home/',
    // You can set any number of default request options.
    'timeout'  => 10.0,
]);

function get_all_lines()
{
    global $client;
    $response = $client->request('GET', 'index.php');
    $code = $response->getStatusCode();
    $bus_lines = array();
    if ($code == 200)
    {
        $body = $response->getBody();
        $document = new Document((string)$body);
        foreach ($document->find('#selBLine a') as $line) {
            $bus_lines[] = $line->text()."\n";
        } 
        print_r($bus_lines);
    file_put_contents('bus.txt',$bus_lines);

    }
}

function index()
{
    $buses = file_get_contents('bus.txt');
    $buses = explode("\n",$buses);
    print_r($buses);
}
function get_bus_direction(string $bus_no)
{
    global $client;
    
    $response = $client->request('GET', 'ajax_rtbus_data.php',[
        'query' => ['act' => 'getLineDir','selBLine'=>$bus_no]
    ]);
    $code = $response->getStatusCode();

    $bus_direction = array();
    if ($code == 200)
    {
        $body = $response->getBody();
        $document = new Document((string)$body);
        foreach ($document->find('a') as $a) {
            $bus_direction[$a->attr('data-uuid')] = $a->text();
        } 
        #print_r(json_encode($bus_direction));
        return json_encode($bus_direction);
    }
};

function get_bus_stop(string $bus_no,string $bus_direction)
{
    global $client;
    #$bus_no = 1;
    #$bus_direction = '5276138694316562750';
    $response = $client->request('GET', 'ajax_rtbus_data.php',[
        'query' => ['act' => 'getDirStation','selBLine'=>$bus_no,'selBDir'=>$bus_direction]
    ]);
    
    $code = $response->getStatusCode();
    
    $bus_stop = array();
    
    if ($code == 200)
    {
        $body = $response->getBody();
        $document = new Document((string)$body);
        foreach ($document->find('a') as $a) {
            $bus_stop[$a->attr('data-seq')] = $a->text();
        } 
        #print_r(json_encode($bus_stop));
        return json_encode($bus_stop);
    }
    
}

function get_bus_details(string $bus_no,string $bus_direction,int $bus_stop)
{
    global $client;
    $response = $client->request('GET', 'ajax_rtbus_data.php',[
        'query' => ['act' => 'busTime',
                    'selBLine'=>$bus_no,'selBDir'=>$bus_direction,'selBStop'=>$bus_stop]
    ]);
    
    $code = $response->getStatusCode();
    
    $bus_details = array();
    
    if ($code == 200)
    {
        $body = $response->getBody();
        $body = (string)$body;
        $json = json_decode($body);
        $body = htmlspecialchars_decode($json->html);
        #echo $body;
        $document = new Document($body);
        $overview = $document->find('div.inquiry_header div.inner article p');
        #var_dump($overview);
        $bus_details['bus_info'] = $overview[0]->text();
        $bus_details['bus_coming'] = $overview[1]->text();
        #print_r($bus_details);
        $stop_details = array();
        $li_stops = $document->find('#cc_stop ul li');
        foreach ($li_stops as $stop) {
            $stop_id = $stop->first('div')->attr('id');
            $stop_details[$stop_id] = array();
            if ($stop->find('i')[0]->attr('class') != null) {
                $stop_details[$stop_id]['has_bus'] = TRUE;
            }
            if ( count($title =$stop->find('span'))>0) {
                $title = $title[0]->attr('title');
                $stop_details[$stop_id]['title'] = $title;
            }
        }
        $bus_details['stops'] = $stop_details;
        #echo (json_encode($bus_details));
        return json_encode($bus_details);
    }
}

if (isset($_GET['busline']) and isset($_GET['busdirection']) and isset($_GET['busstop']))
{
    $bus_no = $_GET['busline'];
    $bus_direction = $_GET['busdirection'];
    $bus_stop = $_GET['busstop'];
    return get_bus_details($bus_no,$bus_direction,$bus_stop);
}

elseif (isset($_GET['busline']) and isset($_GET['busdirection']))
{
    $bus_no = $_GET['busline'];
    $bus_direction = $_GET['busdirection'];
    return get_bus_stop($bus_no,$bus_direction);
}

elseif(isset($_GET['busline']))
{
    $bus_no = $_GET['busline'];
    return get_bus_direction($bus_no);
}


